package com.minaClient.service;

import java.sql.Timestamp;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.minaClient.model.Resources;
import com.minaClient.utils.ToolOS;

public class ResourcesJob {
	/**
	 * 获取资源信息入库
	 */
	public static  Resources resources(){
		String osName = ToolOS.getOsName(); // 获取操作系统类型名称
		String ip = ToolOS.getOsLocalHostIp(); // 获取本机IP
		String hostName = ToolOS.getOsLocalHostName(); // 获取本机名称
		int cpuNumber = ToolOS.getOsCpuNumber(); // 获取CPU数量
		double cpuRatio = ToolOS.getOscpuRatio(); // cpu使用率
		if(cpuRatio < 0){
			cpuRatio = 0;
		}
		
		long phyMemory = ToolOS.getOsPhysicalMemory(); // 物理内存，总的可使用的
		long phyFreeMemory = ToolOS.getOsPhysicalFreeMemory(); // 物理内存，剩余
		
		long jvmTotalMemory = ToolOS.getJvmTotalMemory(); // JVM内存，内存总量
		long jvmFreeMemory = ToolOS.getJvmFreeMemory(); // JVM内存，空闲内存量
		long jvmMaxMemory = ToolOS.getJvmMaxMemory(); // JVM内存，最大内存量
		long gcCount = ToolOS.getJvmGcCount(); // 获取JVM GC次数
		Resources resources = new Resources();
		resources.setOsname(osName);
		resources.setIps(ip);
		resources.setHostname(hostName);
		resources.setCpunumber(cpuNumber);
		resources.setCpuratio(cpuRatio);
		resources.setPhymemory(phyMemory);
		resources.setPhyfreememory(phyFreeMemory);
		resources.setJvmtotalmemory( jvmTotalMemory);
		resources.setJvmfreememory(jvmFreeMemory);
		resources.setJvmmaxmemory(jvmMaxMemory);
		resources.setGccount(gcCount);
		Timestamp d = new Timestamp(System.currentTimeMillis()); 
		resources.setCreatedate(d);
		return resources;
		}
	
	public static void main(String[] args) {
		System.out.println(JSON.toJSONString(ResourcesJob.resources()));
	}
}
